{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "initial_id",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-07T03:23:02.752144200Z",
     "start_time": "2025-06-07T03:23:02.729149900Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   是否减员  年龄               出差情况                      部门  距离（家）  部门.1  \\\n",
      "0     0  37      Travel_Rarely  Research & Development      1     4   \n",
      "1     0  54  Travel_Frequently  Research & Development      1     4   \n",
      "2     1  34  Travel_Frequently  Research & Development      7     3   \n",
      "3     0  39      Travel_Rarely  Research & Development      1     1   \n",
      "4     1  28  Travel_Frequently  Research & Development      1     3   \n",
      "\n",
      "            教育情况  员工编号  环境满意度      性别  ...  关系满意度（家庭）  标准工时 期权水平  工作年限（总计）  \\\n",
      "0  Life Sciences    77      1    Male  ...          3    80    1         7   \n",
      "1  Life Sciences  1245      4  Female  ...          1    80    1        33   \n",
      "2  Life Sciences   147      1    Male  ...          4    80    0         9   \n",
      "3  Life Sciences  1026      4  Female  ...          3    80    1        21   \n",
      "4        Medical  1111      1    Male  ...          1    80    2         1   \n",
      "\n",
      "  上一年培训次数  工作生活平衡  当前在职时长 当前岗位在职时长 上次升职时间  和现任经理时长  \n",
      "0       2       4       7        5      0        7  \n",
      "1       2       1       5        4      1        4  \n",
      "2       3       3       9        7      0        6  \n",
      "3       3       3      21        6     11        8  \n",
      "4       2       3       1        0      0        0  \n",
      "\n",
      "[5 rows x 31 columns]\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1100 entries, 0 to 1099\n",
      "Data columns (total 31 columns):\n",
      " #   Column     Non-Null Count  Dtype \n",
      "---  ------     --------------  ----- \n",
      " 0   是否减员       1100 non-null   int64 \n",
      " 1   年龄         1100 non-null   int64 \n",
      " 2   出差情况       1100 non-null   object\n",
      " 3   部门         1100 non-null   object\n",
      " 4   距离（家）      1100 non-null   int64 \n",
      " 5   部门.1       1100 non-null   int64 \n",
      " 6   教育情况       1100 non-null   object\n",
      " 7   员工编号       1100 non-null   int64 \n",
      " 8   环境满意度      1100 non-null   int64 \n",
      " 9   性别         1100 non-null   object\n",
      " 10  工作投入度      1100 non-null   int64 \n",
      " 11  级别         1100 non-null   int64 \n",
      " 12  工作角色       1100 non-null   object\n",
      " 13  工作满意度      1100 non-null   int64 \n",
      " 14  婚姻状况       1100 non-null   object\n",
      " 15  月收入        1100 non-null   int64 \n",
      " 16  工作的公司数量    1100 non-null   int64 \n",
      " 17  Over18     1100 non-null   object\n",
      " 18  加班         1100 non-null   object\n",
      " 19  加薪百分比      1100 non-null   int64 \n",
      " 20  绩效评级       1100 non-null   int64 \n",
      " 21  关系满意度（家庭）  1100 non-null   int64 \n",
      " 22  标准工时       1100 non-null   int64 \n",
      " 23  期权水平       1100 non-null   int64 \n",
      " 24  工作年限（总计）   1100 non-null   int64 \n",
      " 25  上一年培训次数    1100 non-null   int64 \n",
      " 26  工作生活平衡     1100 non-null   int64 \n",
      " 27  当前在职时长     1100 non-null   int64 \n",
      " 28  当前岗位在职时长   1100 non-null   int64 \n",
      " 29  上次升职时间     1100 non-null   int64 \n",
      " 30  和现任经理时长    1100 non-null   int64 \n",
      "dtypes: int64(23), object(8)\n",
      "memory usage: 266.5+ KB\n"
     ]
    }
   ],
   "source": [
    "import numpy,pandas\n",
    "\n",
    "data=pandas.read_csv(\"./data/train.csv\",encoding='GBK')\n",
    "print(data.head())\n",
    "data.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "edb4e7a0c2a6a255",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-06-07T03:28:40.564897100Z",
     "start_time": "2025-06-07T03:28:40.500511800Z"
    }
   },
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "\"None of [Index(['出差情况'], dtype='object')] are in the [columns]\"",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mKeyError\u001B[0m                                  Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[9], line 13\u001B[0m\n\u001B[0;32m     10\u001B[0m data \u001B[38;5;241m=\u001B[39m pd\u001B[38;5;241m.\u001B[39mget_dummies(data, columns\u001B[38;5;241m=\u001B[39m[\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m年龄分组\u001B[39m\u001B[38;5;124m'\u001B[39m])\n\u001B[0;32m     12\u001B[0m \u001B[38;5;66;03m# 出差情况热编码\u001B[39;00m\n\u001B[1;32m---> 13\u001B[0m data \u001B[38;5;241m=\u001B[39m pd\u001B[38;5;241m.\u001B[39mget_dummies(data, columns\u001B[38;5;241m=\u001B[39m[\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m出差情况\u001B[39m\u001B[38;5;124m'\u001B[39m])\n\u001B[0;32m     15\u001B[0m \u001B[38;5;66;03m#离家距离分组\u001B[39;00m\n\u001B[0;32m     16\u001B[0m bins_distance\u001B[38;5;241m=\u001B[39m[\u001B[38;5;241m0\u001B[39m, \u001B[38;5;241m5\u001B[39m, \u001B[38;5;241m10\u001B[39m, \u001B[38;5;241m15\u001B[39m, \u001B[38;5;241m20\u001B[39m, \u001B[38;5;241m25\u001B[39m]\n",
      "File \u001B[1;32mC:\\software\\anaconda\\Lib\\site-packages\\pandas\\core\\reshape\\encoding.py:169\u001B[0m, in \u001B[0;36mget_dummies\u001B[1;34m(data, prefix, prefix_sep, dummy_na, columns, sparse, drop_first, dtype)\u001B[0m\n\u001B[0;32m    167\u001B[0m     \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mTypeError\u001B[39;00m(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mInput must be a list-like for parameter `columns`\u001B[39m\u001B[38;5;124m\"\u001B[39m)\n\u001B[0;32m    168\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[1;32m--> 169\u001B[0m     data_to_encode \u001B[38;5;241m=\u001B[39m data[columns]\n\u001B[0;32m    171\u001B[0m \u001B[38;5;66;03m# validate prefixes and separator to avoid silently dropping cols\u001B[39;00m\n\u001B[0;32m    172\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mcheck_len\u001B[39m(item, name: \u001B[38;5;28mstr\u001B[39m):\n",
      "File \u001B[1;32mC:\\software\\anaconda\\Lib\\site-packages\\pandas\\core\\frame.py:4108\u001B[0m, in \u001B[0;36mDataFrame.__getitem__\u001B[1;34m(self, key)\u001B[0m\n\u001B[0;32m   4106\u001B[0m     \u001B[38;5;28;01mif\u001B[39;00m is_iterator(key):\n\u001B[0;32m   4107\u001B[0m         key \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mlist\u001B[39m(key)\n\u001B[1;32m-> 4108\u001B[0m     indexer \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mcolumns\u001B[38;5;241m.\u001B[39m_get_indexer_strict(key, \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mcolumns\u001B[39m\u001B[38;5;124m\"\u001B[39m)[\u001B[38;5;241m1\u001B[39m]\n\u001B[0;32m   4110\u001B[0m \u001B[38;5;66;03m# take() does not accept boolean indexers\u001B[39;00m\n\u001B[0;32m   4111\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mgetattr\u001B[39m(indexer, \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mdtype\u001B[39m\u001B[38;5;124m\"\u001B[39m, \u001B[38;5;28;01mNone\u001B[39;00m) \u001B[38;5;241m==\u001B[39m \u001B[38;5;28mbool\u001B[39m:\n",
      "File \u001B[1;32mC:\\software\\anaconda\\Lib\\site-packages\\pandas\\core\\indexes\\base.py:6200\u001B[0m, in \u001B[0;36mIndex._get_indexer_strict\u001B[1;34m(self, key, axis_name)\u001B[0m\n\u001B[0;32m   6197\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[0;32m   6198\u001B[0m     keyarr, indexer, new_indexer \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_reindex_non_unique(keyarr)\n\u001B[1;32m-> 6200\u001B[0m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_raise_if_missing(keyarr, indexer, axis_name)\n\u001B[0;32m   6202\u001B[0m keyarr \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mtake(indexer)\n\u001B[0;32m   6203\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28misinstance\u001B[39m(key, Index):\n\u001B[0;32m   6204\u001B[0m     \u001B[38;5;66;03m# GH 42790 - Preserve name from an Index\u001B[39;00m\n",
      "File \u001B[1;32mC:\\software\\anaconda\\Lib\\site-packages\\pandas\\core\\indexes\\base.py:6249\u001B[0m, in \u001B[0;36mIndex._raise_if_missing\u001B[1;34m(self, key, indexer, axis_name)\u001B[0m\n\u001B[0;32m   6247\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m nmissing:\n\u001B[0;32m   6248\u001B[0m     \u001B[38;5;28;01mif\u001B[39;00m nmissing \u001B[38;5;241m==\u001B[39m \u001B[38;5;28mlen\u001B[39m(indexer):\n\u001B[1;32m-> 6249\u001B[0m         \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mKeyError\u001B[39;00m(\u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mNone of [\u001B[39m\u001B[38;5;132;01m{\u001B[39;00mkey\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m] are in the [\u001B[39m\u001B[38;5;132;01m{\u001B[39;00maxis_name\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m]\u001B[39m\u001B[38;5;124m\"\u001B[39m)\n\u001B[0;32m   6251\u001B[0m     not_found \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mlist\u001B[39m(ensure_index(key)[missing_mask\u001B[38;5;241m.\u001B[39mnonzero()[\u001B[38;5;241m0\u001B[39m]]\u001B[38;5;241m.\u001B[39munique())\n\u001B[0;32m   6252\u001B[0m     \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mKeyError\u001B[39;00m(\u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;132;01m{\u001B[39;00mnot_found\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m not in index\u001B[39m\u001B[38;5;124m\"\u001B[39m)\n",
      "\u001B[1;31mKeyError\u001B[0m: \"None of [Index(['出差情况'], dtype='object')] are in the [columns]\""
     ]
    }
   ],
   "source": [
    "# 数据无异常，无空值\n",
    "# 首先看年龄对离职的影响\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "#年龄分组\n",
    "bins = [18, 25, 30, 35, 40, 45, 50, 60]\n",
    "labels = ['18-25', '26-30', '31-35', '36-40', '41-45', '46-50', '51+']\n",
    "data['年龄分组'] = pd.cut(data['年龄'], bins=bins, labels=labels)\n",
    "#  热编码\n",
    "data = pd.get_dummies(data, columns=['年龄分组'])\n",
    "\n",
    "# 出差情况热编码\n",
    "data = pd.get_dummies(data, columns=['出差情况'])\n",
    "\n",
    "#离家距离分组\n",
    "bins_distance=[0, 5, 10, 15, 20, 25]\n",
    "labels_distance = ['0-5', '6-10', '11-15', '16-20', '21+']\n",
    "data['离家距离分组'] = pd.cut(data['距离'], bins=bins_distance, labels=labels_distance)\n",
    "data=pd.get_dummies(data, columns=['离家距离分组'])\n",
    "# 教育情况热编码\n",
    "data = pd.get_dummies(data, columns=['教育情况'])\n",
    "# 查看不同性别离职率\n",
    "print(data.groupby('性别')['是否减员'].mean())\n",
    "\n",
    "data.info()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "88e8b065c0382546"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
